#对数化
A=read.csv("C:/本科生教学/大三/R/实践3/ADdate实践3-2.csv")
rownames(A)=A[,2]
A=A[,-1]
A=A[,-1]
z=A==0
A[z]=NA
B=log2(A)
colnames(B)
#提取分组信息
a=B[c(grep("B|asym",colnames(B)))]
b=B[c(grep("B|ad",colnames(B)))]
c=B[c(grep("B|ctl",colnames(B)))]
C=cbind(a,b)
D=cbind(c,C)
#判断各组数据是否有至少3个样本是有数值的
type=gsub("[0-9]","",colnames(D))#将行名中的数字用空取代，分3组
table(type)
E=colnames(D)
for (i in (1:64))
{
  pro=unlist(D[i,])#依次取一行
  posctl=type=='ctl'
  ctl=pro[posctl]
  ctlsum=sum(!is.na(ctl))#计算该列的ctl类总数
  posasym=type=='asym'
  asym=pro[posasym]
  asymsum=sum(!is.na(asym))#计算该列的asym类总数
  posad=type=='ad'
  ad=pro[posad]
  adsum=sum(!is.na(ad))#计算该列的sd类总数
  if(ctlsum>3 & asymsum>3 & adsum>3)
  {result=oneway.test(pro~type)#ANOVA检验
  E[i]=result$p.value}
  else E[i]=NA#将结果存入E
}
E



if (!require("BiocManager", quietly = TRUE))
  install.packages("BiocManager")
BiocManager::install("clusterProfiler")
BiocManager::install("org.Hs.eg.db")

library(clusterProfiler)
library(org.Hs.eg.db)
library(ggplot2)
load("C:/本科生教学/大三/R/实践5/class5_volcano.RData")
n= prostat$P<0.05
pro_data = prostat[n,]
allID = pro_data$ID[!is.na(pro_data$ID)]
#基因背景默认全基因
enall1.1= enrichGO(allID,#需要分析的基因
                   OrgDb = "org.Hs.eg.db",#人基因数据库
                   keyType = "SYMBOL",#基因编号类型
                   ont = "BP", #选择功能富集的类型
                   pAdjustMethod = "BH",
                   pvalueCutoff=1)#对生物过程（BP）进行功能富集分析
enall1.2= enrichGO(allID,
                   OrgDb = "org.Hs.eg.db",
                   keyType = "SYMBOL",
                   ont = "CC", 
                   pAdjustMethod = "BH",
                   pvalueCutoff=1)#对细胞组分（CC）进行功能富集分析
enall1.3= enrichGO(allID,
                   OrgDb = "org.Hs.eg.db",
                   keyType = "SYMBOL",
                   ont = "MF", 
                   pAdjustMethod = "BH",
                   pvalueCutoff=1)#对分子功能（MF）进行功能富集分析
#基因背景为列表中的基因
enall2.1= enrichGO(allID,
                   OrgDb = "org.Hs.eg.db",
                   keyType = "SYMBOL",
                   ont = "BP",
                   pAdjustMethod = "BH",
                   pvalueCutoff=1,
                   qvalueCutoff=1,
                   universe=allID)#对生物过程（BP）进行功能富集分析
enall2.2= enrichGO(allID,
                   OrgDb = "org.Hs.eg.db",
                   keyType = "SYMBOL",
                   ont = "CC",
                   pAdjustMethod = "BH",
                   pvalueCutoff=1,
                   qvalueCutoff=1,
                   universe=allID)#对细胞组分（CC）进行功能富集分析
enall2.3= enrichGO(allID,
                   OrgDb = "org.Hs.eg.db",
                   keyType = "SYMBOL",
                   ont = "MF",
                   pAdjustMethod = "BH",
                   pvalueCutoff=1,
                   qvalueCutoff=1,
                   universe=allID)#对分子功能（MF）进行功能富集分析
#结果显示，绘制气泡图展示GO富集分析结果
png("气泡图1.1.png")
dotplot(enall1.1,showCategory = 20)
dev.off()
png("气泡图1.2.png")
dotplot(enall1.2,showCategory = 20)
dev.off()
png("气泡图1.3.png")
dotplot(enall1.3,showCategory = 20)
dev.off()

png("气泡图2.1.png")
dotplot(enall2.1,showCategory = 20)
dev.off()
png("气泡图2.2.png")
dotplot(enall2.2,showCategory = 20)
dev.off()
png("气泡图2.3.png")
dotplot(enall2.3,showCategory = 20)
dev.off()
